<template>
  <el-dialog title="云存储配置" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="config" :rules="dataRule" ref="config" @keyup.enter.native="save()" label-width="120px">
      <template>
        <el-form-item label="域名">
          <el-input v-model="config.domain" placeholder="腾讯云绑定的域名"></el-input>
        </el-form-item>
        <el-form-item label="路径前缀">
          <el-input v-model="config.prefix" placeholder="不设置默认为空"></el-input>
        </el-form-item>
        <el-form-item label="AppId">
          <el-input v-model="config.appId" placeholder="腾讯云AppId"></el-input>
        </el-form-item>
        <el-form-item label="SecretId">
          <el-input v-model="config.secretId" placeholder="腾讯云SecretId"></el-input>
        </el-form-item>
        <el-form-item label="SecretKey">
          <el-input v-model="config.secretKey" placeholder="腾讯云SecretKey"></el-input>
        </el-form-item>
        <el-form-item label="BucketName">
          <el-input v-model="config.bucketName" placeholder="腾讯云BucketName"></el-input>
        </el-form-item>
        <el-form-item label="Bucket所属地区">
          <el-input v-model="config.region" placeholder="如：sh（可选值 ，华南：gz 华北：tj 华东：sh）"></el-input>
        </el-form-item>
      </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="save()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        config: {},
        dataRule: {}
      }
    },
    methods: {
      init() {
        this.visible = true;
        this.$http({
          url: this.$http.adornUrl('/sys/oss/config'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.config = data && data.status === 200 ? data.config : []
        })
      },
      // 表单提交
      save() {
        this.$refs['config'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/sys/oss/saveConfig'),
              method: 'post',
              data: this.$http.adornData(this.config)
            }).then(({data}) => {
              if (data && data.status === 200) {
                this.$message({
                  message: '更新云存储成功!',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.message)
              }
            })
          }
        })
      }
    }
  }
</script>

